import { inject, reactive } from "vue";

const STORE_KEY = '__store__'

const useStore = () => inject(STORE_KEY)

const createStore = (options) => new Store(options)

class Store {
    constructor (options) {
        this._state = reactive({
            data: options.state()
        })
        this._mutations = options.mutations
    }

    get state() {
        return this._state.data
    }

    commit(type, payload) {
        const entry = this._mutations[type]
        entry && entry(this._state, payload)
    }

    install (app) {
        app.provide(STORE_KEY, this)
    }
}

export {createStore, useStore}